﻿// CAddDlg.cpp: 实现文件
//

#include "pch.h"
#include "SalaryManagement.h"
#include "CAddDlg.h"
#include"InfoFile.h"


// CAddDlg

IMPLEMENT_DYNCREATE(CAddDlg, CFormView)

CAddDlg::CAddDlg()
	: CFormView(IDD_ADD)
	, m_name(_T(""))
	, m_val(0)
	, m_num(0)
	, m_val2(0)
	, m_number2(0)
{

}

CAddDlg::~CAddDlg()
{
}

void CAddDlg::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_combo);
	DDX_Text(pDX, IDC_EDIT8, m_name);
	DDX_Text(pDX, IDC_EDIT1, m_val);
	DDX_Text(pDX, IDC_EDIT2, m_num);
	DDX_Text(pDX, IDC_EDIT5, m_val2);
	DDX_Text(pDX, IDC_EDIT7, m_number2);
}

BEGIN_MESSAGE_MAP(CAddDlg, CFormView)
	ON_EN_CHANGE(IDC_EDIT8, &CAddDlg::OnEnChangeEdit8)
	ON_BN_CLICKED(IDC_BUTTON1, &CAddDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON6, &CAddDlg::OnBnClickedButton6)
	ON_CBN_SELCHANGE(IDC_COMBO1, &CAddDlg::OnCbnSelchangeCombo1)
	ON_BN_CLICKED(IDC_BUTTON7, &CAddDlg::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON8, &CAddDlg::OnBnClickedButton8)
END_MESSAGE_MAP()


// CAddDlg 诊断

#ifdef _DEBUG
void CAddDlg::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CAddDlg::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CAddDlg 消息处理程序


void CAddDlg::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	CInfoFile file;
	file.ReadDocline();
	UpdateData(TRUE);
	for (auto it : file.ls) {
		m_combo.AddString(CString(it.name.c_str()));
		m_val = it.price;
		m_combo.SetCurSel(0);
		UpdateData(FALSE);
	}
	m_combo.SetCurSel(0);
	OnCbnSelchangeCombo1();

}


void CAddDlg::OnEnChangeEdit8()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CFormView::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
}


void CAddDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_num <= 0) {
		MessageBox(L"添加失败");
		return;
	}
	CInfoFile file;
	CString str;
	int index = m_combo.GetCurSel();
	m_combo.GetLBText(index, str);
	file.ReadDocline();
	for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end();++it) {
		if (str == CString(it->name.c_str())) {
			it->num += m_num;
			MessageBox(L"购买成功");
			m_num = 0;
			UpdateData(FALSE);
		}
	}
	file.WriteDocline();

	
}


void CAddDlg::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码
	m_num = 0;
	m_combo.SetCurSel(0);
	OnInitialUpdate();
	UpdateData(FALSE);
}


void CAddDlg::OnCbnSelchangeCombo1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	int index = m_combo.GetCurSel();
	m_combo.GetLBText(index, str);
	CInfoFile file;
	file.ReadDocline();
	for (auto it : file.ls) {
		if (str == CString(it.name.c_str())) {
			m_val = it.price;
			UpdateData(FALSE);
		}
	}

}


void CAddDlg::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_name.IsEmpty() || m_number2 <= 0 || m_val2 <= 0) {
		MessageBox(L"添加新商品失败");
		return;
	}
	CInfoFile file;
	file.ReadDocline();
	file.Addline(m_name, m_number2, m_val2);
	file.WriteDocline();


	m_combo.AddString(m_name);
	OnCbnSelchangeCombo1();

	m_name.IsEmpty();
	m_number2 = 0;
	m_val2 = 0;
	MessageBox(L"新商品添加成功");
	
	//OnInitialUpdate();
	
	UpdateData(FALSE);
}


void CAddDlg::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
	m_name.IsEmpty();
	m_number2 = 0;
	m_val2 = 0;
	UpdateData(FALSE);
}
